//
//  UIColor+Extension.swift
//  Supermarket
//
//  Created by liuxm on 2017/8/8.
//  Copyright © 2017年 kushao. All rights reserved.
//

import Foundation
import UIKit

// MARK: - 颜色
extension UIColor {
    class func colorWithHexString(hex:String) ->UIColor {
        var upperHex = hex.trimmingCharacters(in:CharacterSet.whitespacesAndNewlines).uppercased()
        
        if (upperHex.hasPrefix("#")) {
            upperHex = upperHex.substring(from: 1)
        }
        
        if (upperHex.characters.count != 6) {
            return UIColor.red
        }
        
        let rString = upperHex.substring(from: 0, length: 2)
        let gString = upperHex.substring(from: 2, length: 2)
        let bString = upperHex.substring(from: 4, length: 2)
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
    }
}
